﻿//-----------------------------------------------------------------------
// <copyright file="SubFormFieldDefinition.cs" company="Jamie Knight">
// Copyright (c) Jamie Knight. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
using System;
using System.Windows;
using System.Windows.Controls;
using DevKnightlie.WPF.AutoForm.Interfaces;

namespace DevKnightlie.WPF.AutoForm.FieldDefinitions
{

    /// <summary>
    /// Field definition which builds a sub-form in a GroupBox control. The sub-form has its own layout strategy, DataContext and either field definition list or model object form definer
    /// </summary>
    public class SubFormFieldDefinition : BaseFieldDefinition
    {
        /// <summary>
        /// Used to indicate the kind of container the sub-form should be placed in
        /// </summary>
        public enum ContainerTypes
        {
            GroupBox,
            Expander
        }

        #region Properties

        /// <summary>
        /// Sets or returns the layout strategy to use for the sub-form
        /// </summary>
        public ILayoutStrategy LayoutStrategy { get; set; }


        /// <summary>
        /// Sets or returns the field definition list to use for the sub-form
        /// </summary>
        public FieldDefinitionList FieldDefinitionList { get; set; }


        /// <summary>
        /// Sets or returns the form definer object to use for the sub-form
        /// </summary>
        public ModelObjectFormDefiner ModelObjectFormDefiner { get; set; }


        /// <summary>
        /// Sets or returns the data context to use for the sub-form
        /// </summary>
        public object DataContext { get; set; }


        /// <summary>
        /// Sets or returns a value indicating the kind of container control the subform should be in
        /// </summary>
        public ContainerTypes ContainerType { get; set; }


        /// <summary>
        /// Sets or returns a value indicating whether the Expander control is opened by default if <see cref="ContainerType"/> is Expander
        /// </summary>
        public bool ExpanderOpen { get; set; }


        #endregion

        #region Methods

        public override void BuildInputControl()
        {
            // Validate properties
            if (FieldDefinitionList == null && ModelObjectFormDefiner == null)
            {
                throw new InvalidOperationException("Either FieldDefinitionList or ModelObjectFormDefiner must be provided");
            }
            AutoFormPanel autoForm = new AutoFormPanel(LayoutStrategy)
            {
                ModelObjectFormDefiner = ModelObjectFormDefiner,
                FieldDefinitions = FieldDefinitionList
            };
            ContentControl container = null;
            switch (ContainerType)
            {
                case ContainerTypes.GroupBox:
                    container = new GroupBox
                    {
                        VerticalContentAlignment = VerticalAlignment.Stretch,
                        IsTabStop = true,
                        Header = Title,
                        Content = autoForm
                    };
                    break;
                case ContainerTypes.Expander:
                    container = new Expander()
                    {
                        VerticalContentAlignment = VerticalAlignment.Stretch,
                        IsTabStop = true,
                        Header = Title,
                        Content = autoForm,
                        IsExpanded = ExpanderOpen
                    };
                    break;
            }
            InputControl = autoForm;
            ContainerControl = container;
        }

        public override void BuildLabelControl()
        {
            LabelControl = null;
        }

        #endregion
    }
}
